<template>
  <div class="input-box">
    <el-button type="text" @click="dialogVisible = true">{{changeValue}}</el-button>
    <el-dialog
      :title="title"
      :visible.sync="dialogVisible"
      width="30%"
      left
    >
      <p class="name">商品名称：{{goodsName}}</p>
      <div style="display: flex">
        <p v-if="type == 2" style="line-height:38px">库存：</p>
        <p v-else style="line-height:38px">权重值：</p>
        <el-input-number v-model="num" :min="1" :max="99999" label="描述文字"></el-input-number>
      </div>
      <span slot="footer" class="dialog-footer">
    <el-button @click="dialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="update">确 定</el-button>
  </span>
    </el-dialog>
  </div>
</template>

<script>
import {updateSort, updateStorage,updateModelStorage} from '../../../api/supply/suppliergoods'
  export default {
    name: "inputBox",
    props: {
      title: {
        type: String
      },
      goodsId: {
        type: String
      },
      changeValue:{},
      goodsName: {
        type: String
      },
      type: {
        type: Number
      },
      modelStorage: {
        type: Boolean,
        default() {
          return false
        }
      }
    },
    watch: {
      changeValue(newVal,oldVal) {
        this.num = this.changeValue
      }
    },
    data() {
      return {
        dialogVisible: false,
        tips: true,
        num: ''
      }
    },
    created() {
      this.num = this.changeValue
    },
    methods: {
      update() {
        if(this.type == 1) {
          let params = {
            goodsId: this.goodsId,
            sort: this.num
          }
          updateSort(params).then(res => {
            this.$message.success("修改权重成功")
            this.$emit("refresh")
            this.dialogVisible = false
          })
        }
        if(this.type == 2) {
          if(this.modelStorage) {
            console.log("修改规格库存");
            let params = {
              modelId: this.goodsId,
              storage: this.num
            }
            updateModelStorage(params).then(res => {
              this.$message.success("修改库存成功")
              this.$emit("refresh")
              this.dialogVisible = false
            })
          }else {
            console.log('修改商品库存');
            let params = {
              goodsId: this.goodsId,
              storage: this.num
            }
            updateStorage(params).then(res => {
              this.$message.success("修改库存成功")
              this.$emit("refresh")
              this.dialogVisible = false
            })
          }

        }
      }
    }
  }
</script>

<style scoped>
.name {
  margin-bottom: 20px;
  display: flex;
}
</style>
